package ui;

import store.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import lang.*;

/**
 * User: [J0k3r]
 * Date: 01.06.2008
 * Time: 17:39:38
 */
public class Starter extends JFrame
{
    private static DataBase db = new DataBase("mydb");
    private static SQLQueryParser queryParser = new SQLQueryParser(db);

    private void makeScheme() {
        db.createTable("invoice",
                new Column[]{
                        new Column("id", Integer.class, null),
                        new Column("comment", String.class, "no comment"),
                });
        db.createTable("ware",
                new Column[]{
                        new Column("id", Integer.class, null),
                        new Column("name", String.class, null),
                });

        db.createTable("invoice_item",
                new Column[]{
                        new Column("id", Integer.class, null),
                        new Column("invoice_id", Integer.class, null),
                        new Column("ware_id", Integer.class, null),
                });
    }

    public Starter() throws
            HeadlessException {
        super("[J]FuzzyDB");
        
        makeScheme();

        JPanel topPanel = new JPanel(new BorderLayout(5, 5));
        final JTextArea ta = new JTextArea();
        topPanel.add(new JScrollPane(ta), BorderLayout.CENTER);

        JPanel butPan = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        topPanel.add(butPan, BorderLayout.SOUTH);

        JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        pane.setTopComponent(topPanel);

        final JTextArea out = new JTextArea();
        pane.setBottomComponent(new JScrollPane(out));

        final JButton butExec = new JButton("Exec");
        ((JButton)butPan.add(butExec)).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Table table = queryParser.executeQuery(ta.getText().replace("\n", "").replace("\t", ""));
                    int nI = 0;
                    if ((table != null) && (table.getRows().size() > 0)) {
                        for (Column col : table.getColumns())
                            out.setText(out.getText() + col.getColumnName() + ":" + col.getColumnType() + "; ");
                        out.setText(out.getText() + "\n");
                        for (Row r : table.getRows()) {
                            String s = "";
                            for (Field f : r.getFields())
                                s += f.getValue() + "; ";
                            out.setText(out.getText() + "\n" + (++nI) + ") " + s);
                        }
                    } else
                        out.setText("Resultset is empty");
                } catch (Throwable e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(null, e1.getClass().getName() + " " + e1.getMessage());
                }
            }
        });

        final JButton butCls = new JButton("Cls");
        ((JButton)butPan.add(butCls)).addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.setText("");
            }
        });

        ta.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_F5)
                    butExec.doClick();
                if (e.getKeyCode() == KeyEvent.VK_F6) {
                    butCls.doClick();
                    ta.requestFocus();
                }
            }
        });

        this.getContentPane().add(pane, BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Starter();
    }

}
